

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>cup.net.async.conn &mdash; cup 1.7.0 documentation</title>
  

  
  
  
  

  

  
  
    

  

  <link rel="stylesheet" href="../../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../../../genindex/" />
    <link rel="search" title="Search" href="../../../../../search/" /> 

  
  <script src="../../../../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../../../../" class="icon icon-home"> cup
          

          
          </a>

          
            
            
              <div class="version">
                1.7
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../../search/" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../../cup/">cup package</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../../">cup</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../../">Docs</a> &raquo;</li>
        
          <li><a href="../../../../">Module code</a> &raquo;</li>
        
          <li><a href="../../">cup.net</a> &raquo;</li>
        
      <li>cup.net.async.conn</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for cup.net.async.conn</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/env python</span>
<span class="c1"># -*- coding: utf-8 -*</span>
<span class="c1"># Copyright: [CUP] - See LICENSE for details.</span>
<span class="c1"># Authors: Guannan Ma</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">:descrition:</span>
<span class="sd">    connection related module</span>

<span class="sd">    1. There&#39;s only 1 thread reading/receiving data from the interface.</span>

<span class="sd">    2. There might have more than 1 thred writing data into the network</span>
<span class="sd">       queue. 1 thread per context(ip, port).</span>

<span class="sd">    Notice that _do_write will only TRY to send out some data. It might</span>
<span class="sd">    encounter TCP/IP stack full of data in the SEND buffer-queue of</span>
<span class="sd">    the network interface</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">socket</span>
<span class="kn">import</span> <span class="nn">select</span>
<span class="kn">import</span> <span class="nn">errno</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">import</span> <span class="nn">threading</span>
<span class="kn">import</span> <span class="nn">traceback</span>
<span class="k">try</span><span class="p">:</span>
    <span class="kn">import</span> <span class="nn">Queue</span> <span class="k">as</span> <span class="nn">queue</span>  <span class="c1"># pylint: disable=F0401</span>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
    <span class="kn">import</span> <span class="nn">queue</span>   <span class="c1"># pylint: disable=F0401</span>

<span class="kn">import</span> <span class="nn">cup</span>
<span class="kn">from</span> <span class="nn">cup</span> <span class="k">import</span> <span class="n">log</span>
<span class="kn">from</span> <span class="nn">cup</span> <span class="k">import</span> <span class="n">err</span> <span class="k">as</span> <span class="n">cuperr</span>
<span class="kn">from</span> <span class="nn">cup.util</span> <span class="k">import</span> <span class="n">misc</span>
<span class="kn">from</span> <span class="nn">cup.util</span> <span class="k">import</span> <span class="n">threadpool</span>
<span class="kn">from</span> <span class="nn">cup.services</span> <span class="k">import</span> <span class="n">executor</span>
<span class="kn">from</span> <span class="nn">cup.net.async</span> <span class="k">import</span> <span class="n">msg</span> <span class="k">as</span> <span class="n">async_msg</span>
<span class="kn">from</span> <span class="nn">cup.net.async</span> <span class="k">import</span> <span class="n">context</span> <span class="k">as</span> <span class="n">sockcontext</span>


<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span>
    <span class="s1">&#39;CConnectionManager&#39;</span>
<span class="p">]</span>


<span class="k">def</span> <span class="nf">_try_get_peer_info</span><span class="p">(</span><span class="n">sock</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    get peer info</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">peer</span> <span class="o">=</span> <span class="n">sock</span><span class="o">.</span><span class="n">getpeername</span><span class="p">()</span>
    <span class="k">except</span> <span class="n">socket</span><span class="o">.</span><span class="n">error</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
        <span class="n">peer</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;Error happened&#39;</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">error</span><span class="p">))</span>
    <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
        <span class="n">peer</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;_try_get_peer_info error happend&#39;</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">error</span><span class="p">))</span>
    <span class="k">return</span> <span class="n">peer</span>


<span class="c1"># pylint: disable=R0902</span>
<div class="viewcode-block" id="CConnectionManager"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.conn.CConnectionManager">[docs]</a><span class="k">class</span> <span class="nc">CConnectionManager</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        connaddr. Convert ip:port  into a 64-bit hex.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">NET_RW_SIZE</span> <span class="o">=</span> <span class="mi">131072</span>
    <span class="c1"># NET_RW_SIZE = 4096</span>

<div class="viewcode-block" id="CConnectionManager.QueueError"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.conn.CConnectionManager.QueueError">[docs]</a>    <span class="k">class</span> <span class="nc">QueueError</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        internal queue error for CConnectionManager class</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">msg</span><span class="p">):</span>
            <span class="nb">super</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_msg</span> <span class="o">=</span> <span class="n">msg</span>

        <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_msg</span></div>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ip</span><span class="p">,</span> <span class="n">bindport</span><span class="p">,</span> <span class="n">thdpool_param</span><span class="p">):</span>
        <span class="c1">#  TODO: Close idle socket after 30 mins with no data sent or received.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_conns</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_bind_port</span> <span class="o">=</span> <span class="n">bindport</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_bind_ip</span> <span class="o">=</span> <span class="n">ip</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_epoll</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">epoll</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_stopsign</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_bind_sock</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_fileno2context</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_context2fileno_peer</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_peer2context</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">min_thds</span><span class="p">,</span> <span class="n">max_thds</span> <span class="o">=</span> <span class="n">thdpool_param</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_thdpool</span> <span class="o">=</span> <span class="n">threadpool</span><span class="o">.</span><span class="n">ThreadPool</span><span class="p">(</span>
            <span class="n">min_thds</span><span class="p">,</span> <span class="n">max_thds</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;network_write_read&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_recv_queue</span> <span class="o">=</span> <span class="n">queue</span><span class="o">.</span><span class="n">PriorityQueue</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_stopsign</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_recv_msg_ind</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_mlock</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Lock</span><span class="p">()</span>
        <span class="c1"># _needack_context_queue</span>
        <span class="c1"># infinite queue  TODO:  may change it in the future</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_needack_context_queue</span> <span class="o">=</span> <span class="n">queue</span><span class="o">.</span><span class="n">Queue</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_dict_lock</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Lock</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_needack_context_dict</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_executor</span> <span class="o">=</span> <span class="n">executor</span><span class="o">.</span><span class="n">ExecutionService</span><span class="p">(</span>
            <span class="c1">#int(&#39;queue_exec_thdnum&#39;),    # todo num?</span>
            <span class="c1">#int(&#39;queue_delay_exe_thdnum&#39;)   # todo num?</span>
            <span class="mi">3</span><span class="p">,</span>
            <span class="mi">4</span>
        <span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_type_man</span> <span class="o">=</span> <span class="n">async_msg</span><span class="o">.</span><span class="n">CMsgType</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_type_man</span><span class="o">.</span><span class="n">register_types</span><span class="p">(</span><span class="n">async_msg</span><span class="o">.</span><span class="n">MSG_TYPE2NUM</span><span class="p">)</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_set_sock_params</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">sock</span><span class="p">):</span>
        <span class="n">cup</span><span class="o">.</span><span class="n">net</span><span class="o">.</span><span class="n">set_sock_keepalive_linux</span><span class="p">(</span><span class="n">sock</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
        <span class="n">cup</span><span class="o">.</span><span class="n">net</span><span class="o">.</span><span class="n">set_sock_linger</span><span class="p">(</span><span class="n">sock</span><span class="p">)</span>
        <span class="n">cup</span><span class="o">.</span><span class="n">net</span><span class="o">.</span><span class="n">set_sock_quickack</span><span class="p">(</span><span class="n">sock</span><span class="p">)</span>
        <span class="n">cup</span><span class="o">.</span><span class="n">net</span><span class="o">.</span><span class="n">set_sock_reusable</span><span class="p">(</span><span class="n">sock</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_set_sock_nonblocking</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">sock</span><span class="p">):</span>
        <span class="n">sock</span><span class="o">.</span><span class="n">setblocking</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_epoll_write_params</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">(</span><span class="n">select</span><span class="o">.</span><span class="n">EPOLLET</span> <span class="o">|</span> <span class="n">select</span><span class="o">.</span><span class="n">EPOLLOUT</span> <span class="o">|</span> <span class="n">select</span><span class="o">.</span><span class="n">EPOLLERR</span><span class="p">)</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_epoll_read_params</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">(</span><span class="n">select</span><span class="o">.</span><span class="n">EPOLLET</span> <span class="o">|</span> <span class="n">select</span><span class="o">.</span><span class="n">EPOLLIN</span> <span class="o">|</span> <span class="n">select</span><span class="o">.</span><span class="n">EPOLLERR</span><span class="p">)</span>

<div class="viewcode-block" id="CConnectionManager.get_needack_dict"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.conn.CConnectionManager.get_needack_dict">[docs]</a>    <span class="k">def</span> <span class="nf">get_needack_dict</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get neekack dict</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_needack_context_dict</span></div>

<div class="viewcode-block" id="CConnectionManager.push_msg2needack_queue"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.conn.CConnectionManager.push_msg2needack_queue">[docs]</a>    <span class="k">def</span> <span class="nf">push_msg2needack_queue</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">msg</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get neekack dict</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;push ack ok msg into needack_queue.&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_needack_context_queue</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span></div>

<div class="viewcode-block" id="CConnectionManager.bind"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.conn.CConnectionManager.bind">[docs]</a>    <span class="k">def</span> <span class="nf">bind</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        bind the ip:port</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">sock</span> <span class="o">=</span> <span class="n">socket</span><span class="o">.</span><span class="n">socket</span><span class="p">(</span><span class="n">socket</span><span class="o">.</span><span class="n">AF_INET</span><span class="p">,</span> <span class="n">socket</span><span class="o">.</span><span class="n">SOCK_STREAM</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_set_sock_params</span><span class="p">(</span><span class="n">sock</span><span class="p">)</span>
        <span class="n">sock</span><span class="o">.</span><span class="n">bind</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">_bind_ip</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_bind_port</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_set_sock_nonblocking</span><span class="p">(</span><span class="n">sock</span><span class="p">)</span>
        <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span>
            <span class="s1">&#39;bind port info:(ip:</span><span class="si">%s</span><span class="s1">, port:</span><span class="si">%s</span><span class="s1">)&#39;</span> <span class="o">%</span> <span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_bind_ip</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_bind_port</span>
            <span class="p">)</span>
        <span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_epoll</span><span class="o">.</span><span class="n">register</span><span class="p">(</span>
            <span class="n">sock</span><span class="o">.</span><span class="n">fileno</span><span class="p">(),</span>
            <span class="n">select</span><span class="o">.</span><span class="n">EPOLLIN</span> <span class="o">|</span> <span class="n">select</span><span class="o">.</span><span class="n">EPOLLET</span> <span class="o">|</span> <span class="n">select</span><span class="o">.</span><span class="n">EPOLLOUT</span> <span class="o">|</span> <span class="n">select</span><span class="o">.</span><span class="n">EPOLLERR</span>
        <span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_bind_sock</span> <span class="o">=</span> <span class="n">sock</span></div>

<div class="viewcode-block" id="CConnectionManager.push_msg2sendqueue"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.conn.CConnectionManager.push_msg2sendqueue">[docs]</a>    <span class="k">def</span> <span class="nf">push_msg2sendqueue</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">msg</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        push msg into the send queue</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">ret</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">if</span> <span class="n">msg</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">log</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s1">&#39;put a None into msg send queue. return&#39;</span><span class="p">)</span>
            <span class="n">ret</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
            <span class="k">return</span> <span class="n">ret</span>
        <span class="n">valid</span><span class="p">,</span> <span class="n">errmsg</span> <span class="o">=</span> <span class="n">msg</span><span class="o">.</span><span class="n">is_valid4send</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">valid</span><span class="p">:</span>
            <span class="n">log</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s1">&#39;failed to send msg as msg is not valid to send&#39;</span><span class="p">)</span>
            <span class="k">return</span> <span class="o">-</span><span class="mi">1</span>
        <span class="n">flag</span> <span class="o">=</span> <span class="n">msg</span><span class="o">.</span><span class="n">get_flag</span><span class="p">()</span>
        <span class="n">peer</span> <span class="o">=</span> <span class="n">msg</span><span class="o">.</span><span class="n">get_to_addr</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">new_created</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="n">context</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">sock</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">msg</span><span class="p">,</span> <span class="n">async_msg</span><span class="o">.</span><span class="n">CNeedAckMsg</span><span class="p">):</span>
            <span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;CNeedAckMsg is to be sent. msg_type:</span><span class="si">%d</span><span class="s1">,&#39;</span>
                <span class="s1">&#39;msg_flag:</span><span class="si">%d</span><span class="s1">, msg_dest:</span><span class="si">%s</span><span class="s1">, uniqid:</span><span class="si">%d</span><span class="s1">&#39;</span> <span class="o">%</span>
                <span class="p">(</span>
                    <span class="n">msg</span><span class="o">.</span><span class="n">get_msg_type</span><span class="p">(),</span>
                    <span class="n">msg</span><span class="o">.</span><span class="n">get_flag</span><span class="p">(),</span>
                    <span class="nb">str</span><span class="p">(</span><span class="n">msg</span><span class="o">.</span><span class="n">get_to_addr</span><span class="p">()),</span>
                    <span class="n">msg</span><span class="o">.</span><span class="n">get_uniq_id</span><span class="p">()</span>
                <span class="p">)</span>
            <span class="p">)</span>
            <span class="c1"># no need head by default</span>
            <span class="c1"># msg.set_need_head(b_need=False)</span>
            <span class="k">if</span> <span class="n">msg</span><span class="o">.</span><span class="n">get_last_retry_time</span><span class="p">()</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">msg</span><span class="o">.</span><span class="n">set_last_retry_time</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span>
            <span class="c1"># if not in the self._needack_context_dict</span>
            <span class="k">if</span> <span class="n">msg</span><span class="o">.</span><span class="n">get_retry_times</span><span class="p">()</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_needack_context_queue</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">context</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_peer2context</span><span class="p">[</span><span class="n">peer</span><span class="p">]</span>
        <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
            <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;To create a new context for the sock:</span><span class="si">{0}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="n">peer</span><span class="p">)</span>
            <span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_mlock</span><span class="o">.</span><span class="n">acquire</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">peer</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_peer2context</span><span class="p">:</span>
                <span class="n">sock</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">peer</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">sock</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="n">context</span> <span class="o">=</span> <span class="n">sockcontext</span><span class="o">.</span><span class="n">CConnContext</span><span class="p">()</span>
                    <span class="n">context</span><span class="o">.</span><span class="n">set_conn_man</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
                    <span class="n">context</span><span class="o">.</span><span class="n">set_sock</span><span class="p">(</span><span class="n">sock</span><span class="p">)</span>
                    <span class="n">context</span><span class="o">.</span><span class="n">set_peerinfo</span><span class="p">(</span><span class="n">peer</span><span class="p">)</span>
                    <span class="n">fileno</span> <span class="o">=</span> <span class="n">sock</span><span class="o">.</span><span class="n">fileno</span><span class="p">()</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_peer2context</span><span class="p">[</span><span class="n">peer</span><span class="p">]</span> <span class="o">=</span> <span class="n">context</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_fileno2context</span><span class="p">[</span><span class="n">fileno</span><span class="p">]</span> <span class="o">=</span> <span class="n">context</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_context2fileno_peer</span><span class="p">[</span><span class="n">context</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">fileno</span><span class="p">,</span> <span class="n">peer</span><span class="p">)</span>
                    <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;created context for the new sock&#39;</span><span class="p">)</span>
                    <span class="n">ret</span> <span class="o">=</span> <span class="mi">0</span>
                    <span class="k">try</span><span class="p">:</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">_epoll</span><span class="o">.</span><span class="n">register</span><span class="p">(</span>
                            <span class="n">sock</span><span class="o">.</span><span class="n">fileno</span><span class="p">(),</span> <span class="bp">self</span><span class="o">.</span><span class="n">_epoll_write_params</span><span class="p">()</span>
                        <span class="p">)</span>
                    <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>  <span class="c1"># pylint: disable=W0703</span>
                        <span class="n">log</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span>
                            <span class="s1">&#39;failed to register the socket fileno, err_msg:</span><span class="si">%s</span><span class="s1">,&#39;</span>
                            <span class="s1">&#39;perinfo:</span><span class="si">%s</span><span class="s1">:</span><span class="si">%s</span><span class="s1">. To epoll modify it&#39;</span> <span class="o">%</span>
                            <span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">error</span><span class="p">),</span> <span class="n">peer</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">peer</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
                        <span class="p">)</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">_epoll</span><span class="o">.</span><span class="n">modify</span><span class="p">(</span>
                            <span class="n">sock</span><span class="o">.</span><span class="n">fileno</span><span class="p">(),</span> <span class="bp">self</span><span class="o">.</span><span class="n">_epoll_write_params</span><span class="p">()</span>
                        <span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">log</span><span class="o">.</span><span class="n">error</span><span class="p">(</span>
                        <span class="s1">&#39;failed to post msg. Connect failed. peer info:</span><span class="si">{0}</span><span class="s1">.&#39;</span>
                        <span class="s1">&#39; msg_type:</span><span class="si">{1}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                            <span class="nb">str</span><span class="p">(</span><span class="n">peer</span><span class="p">),</span> <span class="n">msg</span><span class="o">.</span><span class="n">get_msg_type</span><span class="p">()</span>
                        <span class="p">)</span>
                    <span class="p">)</span>
                    <span class="n">ret</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">context</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_peer2context</span><span class="p">[</span><span class="n">peer</span><span class="p">]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_mlock</span><span class="o">.</span><span class="n">release</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">context</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_peer2context</span><span class="p">[</span><span class="n">peer</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">ret</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">ret</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">context</span><span class="o">.</span><span class="n">is_detroying</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">context</span><span class="o">.</span><span class="n">put_msg</span><span class="p">(</span><span class="n">flag</span><span class="p">,</span> <span class="n">msg</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">ret</span> <span class="o">=</span> <span class="mi">0</span>
                <span class="c1"># set up last modify</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">ret</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
            <span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;start handle new send.&#39;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_handle_new_send</span><span class="p">(</span><span class="n">context</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">ret</span></div>

<div class="viewcode-block" id="CConnectionManager.connect"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.conn.CConnectionManager.connect">[docs]</a>    <span class="k">def</span> <span class="nf">connect</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">peer</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :param peer:</span>
<span class="sd">            ip:port</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;to connect to peer:</span><span class="si">{0}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">peer</span><span class="p">))</span>
        <span class="n">sock</span> <span class="o">=</span> <span class="n">socket</span><span class="o">.</span><span class="n">socket</span><span class="p">(</span><span class="n">socket</span><span class="o">.</span><span class="n">AF_INET</span><span class="p">,</span> <span class="n">socket</span><span class="o">.</span><span class="n">SOCK_STREAM</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_set_sock_params</span><span class="p">(</span><span class="n">sock</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">ret</span> <span class="o">=</span> <span class="n">sock</span><span class="o">.</span><span class="n">connect_ex</span><span class="p">(</span><span class="n">peer</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">ret</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">log</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s1">&#39;connect failed, peer:</span><span class="si">{0}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">peer</span><span class="p">))</span>
                <span class="k">return</span> <span class="kc">None</span>
            <span class="k">if</span> <span class="n">sock</span><span class="o">.</span><span class="n">getpeername</span><span class="p">()</span> <span class="o">==</span> <span class="n">sock</span><span class="o">.</span><span class="n">getsockname</span><span class="p">():</span>
                <span class="n">log</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s1">&#39;connect failed, seems connected to self&#39;</span><span class="p">)</span>
                <span class="n">sock</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
                <span class="k">return</span> <span class="kc">None</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_set_sock_nonblocking</span><span class="p">(</span><span class="n">sock</span><span class="p">)</span>
            <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;connect peer success&#39;</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">sock</span>
        <span class="k">except</span> <span class="n">socket</span><span class="o">.</span><span class="n">error</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
            <span class="n">log</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span>
                <span class="s1">&#39;failed to connect to </span><span class="si">%s</span><span class="s1">:</span><span class="si">%s</span><span class="s1">. Error:</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span>
                <span class="p">(</span><span class="n">peer</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">peer</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="nb">str</span><span class="p">(</span><span class="n">error</span><span class="p">))</span>
            <span class="p">)</span>
            <span class="n">sock</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
            <span class="k">return</span> <span class="kc">None</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">sock</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
            <span class="k">return</span> <span class="kc">None</span></div>

    <span class="k">def</span> <span class="nf">_handle_new_conn</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">newsock</span><span class="p">,</span> <span class="n">peer</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_mlock</span><span class="o">.</span><span class="n">acquire</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_set_sock_params</span><span class="p">(</span><span class="n">newsock</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_set_sock_nonblocking</span><span class="p">(</span><span class="n">newsock</span><span class="p">)</span>
        <span class="n">context</span> <span class="o">=</span> <span class="n">sockcontext</span><span class="o">.</span><span class="n">CConnContext</span><span class="p">()</span>
        <span class="n">context</span><span class="o">.</span><span class="n">set_sock</span><span class="p">(</span><span class="n">newsock</span><span class="p">)</span>
        <span class="n">context</span><span class="o">.</span><span class="n">set_conn_man</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="n">context</span><span class="o">.</span><span class="n">set_peerinfo</span><span class="p">(</span><span class="n">peer</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_epoll</span><span class="o">.</span><span class="n">register</span><span class="p">(</span>
            <span class="n">newsock</span><span class="o">.</span><span class="n">fileno</span><span class="p">(),</span> <span class="n">select</span><span class="o">.</span><span class="n">EPOLLIN</span> <span class="o">|</span> <span class="n">select</span><span class="o">.</span><span class="n">EPOLLET</span> <span class="o">|</span> <span class="n">select</span><span class="o">.</span><span class="n">EPOLLERR</span>
        <span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_fileno2context</span><span class="p">[</span><span class="n">newsock</span><span class="o">.</span><span class="n">fileno</span><span class="p">()]</span> <span class="o">=</span> <span class="n">context</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_peer2context</span><span class="p">[</span><span class="n">peer</span><span class="p">]</span> <span class="o">=</span> <span class="n">context</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_context2fileno_peer</span><span class="p">[</span><span class="n">context</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">newsock</span><span class="o">.</span><span class="n">fileno</span><span class="p">(),</span> <span class="n">peer</span><span class="p">)</span>
        <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;a new connection: </span><span class="si">%s</span><span class="s1">:</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">peer</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">peer</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_mlock</span><span class="o">.</span><span class="n">release</span><span class="p">()</span>

<div class="viewcode-block" id="CConnectionManager.cleanup_error_context"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.conn.CConnectionManager.cleanup_error_context">[docs]</a>    <span class="k">def</span> <span class="nf">cleanup_error_context</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">context</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;clean up error context&quot;&quot;&quot;</span>
        <span class="k">def</span> <span class="nf">_cleanup_context</span><span class="p">(</span><span class="n">send_queue</span><span class="p">,</span> <span class="n">peerinfo</span><span class="p">):</span>
            <span class="sd">&quot;&quot;&quot;cleanup context&quot;&quot;&quot;</span>
            <span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;to cleanup socket, peer:</span><span class="si">{0}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">peerinfo</span><span class="p">))</span>
            <span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span>
                <span class="s1">&#39;cleanup: send_queue of socket size:</span><span class="si">{0}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                    <span class="n">send_queue</span><span class="o">.</span><span class="n">qsize</span><span class="p">()</span>
                <span class="p">)</span>
            <span class="p">)</span>
            <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">item</span> <span class="o">=</span> <span class="n">send_queue</span><span class="o">.</span><span class="n">get_nowait</span><span class="p">()</span>
                    <span class="n">msg</span> <span class="o">=</span> <span class="n">item</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
                    <span class="k">del</span> <span class="n">msg</span>
                <span class="k">except</span> <span class="n">queue</span><span class="o">.</span><span class="n">Empty</span><span class="p">:</span>
                    <span class="k">break</span>
        <span class="k">if</span> <span class="n">context</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_mlock</span><span class="o">.</span><span class="n">acquire</span><span class="p">()</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">peerinfo</span> <span class="o">=</span> <span class="n">context</span><span class="o">.</span><span class="n">get_peerinfo</span><span class="p">()</span>
            <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span>
                <span class="s1">&#39;handle socket reset by peer, to close the socket:</span><span class="si">%s</span><span class="s1">:</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span>
                <span class="p">(</span><span class="n">peerinfo</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">peerinfo</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
            <span class="p">)</span>
            <span class="n">fileno_peer</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_context2fileno_peer</span><span class="p">[</span><span class="n">context</span><span class="p">]</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">sock</span> <span class="o">=</span> <span class="n">context</span><span class="o">.</span><span class="n">get_sock</span><span class="p">()</span>
                <span class="n">sock</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
                <span class="n">context</span><span class="o">.</span><span class="n">set_sock</span><span class="p">(</span><span class="kc">None</span><span class="p">)</span>
            <span class="k">except</span> <span class="n">socket</span><span class="o">.</span><span class="n">error</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
                <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span>
                    <span class="s1">&#39;failed to close the socket, err_msg:</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="nb">str</span><span class="p">(</span><span class="n">error</span><span class="p">)</span>
                <span class="p">)</span>
            <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
                <span class="n">log</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s1">&#39;failed to close socket:</span><span class="si">{0}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">error</span><span class="p">))</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_epoll</span><span class="o">.</span><span class="n">unregister</span><span class="p">(</span><span class="n">fileno_peer</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
            <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>  <span class="c1"># pylint: disable=W0703</span>
                <span class="n">log</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span>
                    <span class="s1">&#39;epoll unregister error:</span><span class="si">%s</span><span class="s1">, peerinfo:</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span>
                    <span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">error</span><span class="p">),</span> <span class="nb">str</span><span class="p">(</span><span class="n">fileno_peer</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span>
                <span class="p">)</span>
            <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">_fileno2context</span><span class="p">[</span><span class="n">fileno_peer</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span>
            <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">_peer2context</span><span class="p">[</span><span class="n">fileno_peer</span><span class="p">[</span><span class="mi">1</span><span class="p">]]</span>
            <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">_context2fileno_peer</span><span class="p">[</span><span class="n">context</span><span class="p">]</span>
            <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;socket closed&#39;</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
            <span class="k">pass</span>
        <span class="k">finally</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_mlock</span><span class="o">.</span><span class="n">release</span><span class="p">()</span>
        <span class="c1"># pylint: disable=W0212</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_thdpool</span><span class="o">.</span><span class="n">add_1job</span><span class="p">(</span><span class="n">_cleanup_context</span><span class="p">,</span> <span class="n">context</span><span class="o">.</span><span class="n">_send_queue</span><span class="p">,</span> <span class="n">peerinfo</span><span class="p">)</span></div>

<div class="viewcode-block" id="CConnectionManager.close_socket"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.conn.CConnectionManager.close_socket">[docs]</a>    <span class="k">def</span> <span class="nf">close_socket</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">msg</span><span class="p">,</span> <span class="n">recv_socket</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        close socket by msg</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">peer</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">recv_socket</span><span class="p">:</span>
                <span class="n">peer</span> <span class="o">=</span> <span class="n">msg</span><span class="o">.</span><span class="n">get_to_addr</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">peer</span> <span class="o">=</span> <span class="n">msg</span><span class="o">.</span><span class="n">get_from_addr</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>
            <span class="n">context</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_peer2context</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">peer</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">context</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">cleanup_error_context</span><span class="p">(</span><span class="n">context</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">log</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s1">&#39;conn manager close socket failed:</span><span class="si">{0}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                    <span class="n">peer</span><span class="p">)</span>
                <span class="p">)</span>
        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">err</span><span class="p">:</span>
            <span class="n">log</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s1">&#39;failed to close socket:</span><span class="si">{1}</span><span class="s1">, recv_socket:</span><span class="si">{0}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="n">recv_socket</span><span class="p">,</span> <span class="n">err</span><span class="p">)</span>
            <span class="p">)</span>
        <span class="k">return</span></div>

<div class="viewcode-block" id="CConnectionManager.poll"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.conn.CConnectionManager.poll">[docs]</a>    <span class="k">def</span> <span class="nf">poll</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        start to poll</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_thdpool</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_executor</span><span class="o">.</span><span class="n">run</span><span class="p">()</span>
        <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;thdpool and executor start&#39;</span><span class="p">)</span>
        <span class="n">misc</span><span class="o">.</span><span class="n">check_not_none</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_bind_sock</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_bind_sock</span><span class="o">.</span><span class="n">listen</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_executor</span><span class="o">.</span><span class="n">delay_exec</span><span class="p">(</span>
            <span class="mi">2</span><span class="p">,</span>   <span class="c1"># todo set the check_time to ?</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">do_check_msg_ack_loop</span><span class="p">,</span>
            <span class="n">urgency</span><span class="o">=</span><span class="n">executor</span><span class="o">.</span><span class="n">URGENCY_HIGH</span>
        <span class="p">)</span>
        <span class="k">while</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_stopsign</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">events</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_epoll</span><span class="o">.</span><span class="n">poll</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">IOError</span> <span class="k">as</span> <span class="n">err</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">err</span><span class="o">.</span><span class="n">errno</span> <span class="o">==</span> <span class="n">errno</span><span class="o">.</span><span class="n">EINTR</span><span class="p">:</span>
                    <span class="k">return</span>
                <span class="k">raise</span> <span class="n">err</span>
            <span class="c1"># log.debug(&#39;start to poll&#39;)</span>
            <span class="k">for</span> <span class="n">fileno</span><span class="p">,</span> <span class="n">event</span> <span class="ow">in</span> <span class="n">events</span><span class="p">:</span>
                <span class="c1"># if it comes from the listen port, new conn</span>
                <span class="k">if</span> <span class="n">fileno</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_bind_sock</span><span class="o">.</span><span class="n">fileno</span><span class="p">():</span>
                    <span class="n">newsock</span><span class="p">,</span> <span class="n">addr</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_bind_sock</span><span class="o">.</span><span class="n">accept</span><span class="p">()</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_handle_new_conn</span><span class="p">(</span><span class="n">newsock</span><span class="p">,</span> <span class="n">addr</span><span class="p">)</span>
                <span class="k">elif</span> <span class="n">event</span> <span class="o">&amp;</span> <span class="n">select</span><span class="o">.</span><span class="n">EPOLLIN</span><span class="p">:</span>
                    <span class="k">try</span><span class="p">:</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">_handle_new_recv</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_fileno2context</span><span class="p">[</span><span class="n">fileno</span><span class="p">])</span>
                    <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
                        <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;socket already closed&#39;</span><span class="p">)</span>
                <span class="k">elif</span> <span class="n">event</span> <span class="o">&amp;</span> <span class="n">select</span><span class="o">.</span><span class="n">EPOLLOUT</span><span class="p">:</span>
                    <span class="k">try</span><span class="p">:</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">_handle_new_send</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_fileno2context</span><span class="p">[</span><span class="n">fileno</span><span class="p">])</span>
                    <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
                        <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;socket already closed&#39;</span><span class="p">)</span>
                <span class="k">elif</span> <span class="p">(</span><span class="n">event</span> <span class="o">&amp;</span> <span class="n">select</span><span class="o">.</span><span class="n">EPOLLHUP</span><span class="p">)</span> <span class="ow">or</span> <span class="p">(</span><span class="n">event</span> <span class="o">&amp;</span> <span class="n">select</span><span class="o">.</span><span class="n">EPOLLERR</span><span class="p">):</span>
                    <span class="c1"># FIXME: consider if we need to release net msg resources</span>
                    <span class="k">if</span> <span class="n">event</span> <span class="o">&amp;</span> <span class="n">select</span><span class="o">.</span><span class="n">EPOLLHUP</span><span class="p">:</span>
                        <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;--EPOLLHUP--&#39;</span><span class="p">)</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;--EPOLLERR--&#39;</span><span class="p">)</span>
                    <span class="k">try</span><span class="p">:</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">cleanup_error_context</span><span class="p">(</span>
                            <span class="bp">self</span><span class="o">.</span><span class="n">_fileno2context</span><span class="p">[</span><span class="n">fileno</span><span class="p">]</span>
                        <span class="p">)</span>
                    <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
                        <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;socket already closed&#39;</span><span class="p">)</span></div>

<div class="viewcode-block" id="CConnectionManager.dump_stats"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.conn.CConnectionManager.dump_stats">[docs]</a>    <span class="k">def</span> <span class="nf">dump_stats</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        dump stats</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_thdpool</span><span class="o">.</span><span class="n">dump_stats</span><span class="p">()</span></div>

    <span class="k">def</span> <span class="nf">_async_stop</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">force_stop</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        to async stop thread pool and executor&quot;&quot;&quot;</span>
        <span class="n">stop_pool</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">(</span>
            <span class="n">target</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_thdpool</span><span class="o">.</span><span class="n">stop</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">force_stop</span><span class="p">,</span> <span class="p">)</span>
        <span class="p">)</span>
        <span class="n">stop_pool</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
        <span class="n">stop_executor</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">(</span>
            <span class="n">target</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_executor</span><span class="o">.</span><span class="n">stop</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">force_stop</span><span class="p">,</span> <span class="p">)</span>
        <span class="p">)</span>
        <span class="n">stop_executor</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
        <span class="n">stop_pool</span><span class="o">.</span><span class="n">join</span><span class="p">()</span>
        <span class="n">stop_executor</span><span class="o">.</span><span class="n">join</span><span class="p">()</span>

<div class="viewcode-block" id="CConnectionManager.stop"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.conn.CConnectionManager.stop">[docs]</a>    <span class="k">def</span> <span class="nf">stop</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">force_stop</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        stop the connection manager</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;to stop the connection manager&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_stopsign</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_async_stop</span><span class="p">(</span><span class="n">force_stop</span><span class="p">)</span>
        <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;connection manager stopped&#39;</span><span class="p">)</span></div>

<div class="viewcode-block" id="CConnectionManager.get_recv_msg_ind"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.conn.CConnectionManager.get_recv_msg_ind">[docs]</a>    <span class="k">def</span> <span class="nf">get_recv_msg_ind</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get recv msg ind</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_recv_msg_ind</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_recv_msg_ind</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="k">return</span> <span class="n">tmp</span></div>

<div class="viewcode-block" id="CConnectionManager.get_recv_queue"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.conn.CConnectionManager.get_recv_queue">[docs]</a>    <span class="k">def</span> <span class="nf">get_recv_queue</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get recving_msg queue</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_recv_queue</span></div>

<div class="viewcode-block" id="CConnectionManager.get_recv_msg"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.conn.CConnectionManager.get_recv_msg">[docs]</a>    <span class="k">def</span> <span class="nf">get_recv_msg</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get recv msg from queue</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;to fetch a msg from recv_queue for handle function&#39;</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="c1"># should use block-mode, othwersie the while loop in the upper</span>
            <span class="c1"># code scope will crazily occupy a full cpu-core capacity.</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_recv_queue</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">block</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="mf">0.5</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span>
        <span class="k">except</span> <span class="n">queue</span><span class="o">.</span><span class="n">Empty</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">except</span> <span class="ne">TypeError</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
            <span class="n">log</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s1">&#39;type error, seems received SIGTERM, err:</span><span class="si">{0}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="n">error</span><span class="p">)</span>
            <span class="p">)</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="s1">&#39;Catch a error that I cannot handle, err_msg:</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">error</span>
            <span class="n">log</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
            <span class="n">log</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">error</span><span class="p">))</span>
            <span class="k">raise</span> <span class="n">CConnectionManager</span><span class="o">.</span><span class="n">QueueError</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">msg</span></div>

    <span class="k">def</span> <span class="nf">_handle_new_recv</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">context</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_thdpool</span><span class="o">.</span><span class="n">add_1job</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">read</span><span class="p">,</span> <span class="n">context</span><span class="p">)</span>
        <span class="c1"># self.read(context)</span>

    <span class="k">def</span> <span class="nf">_finish_read_callback</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">succ</span><span class="p">,</span> <span class="n">result</span><span class="p">):</span>
        <span class="n">context</span> <span class="o">=</span> <span class="n">result</span>
        <span class="k">if</span> <span class="n">context</span><span class="o">.</span><span class="n">is_detroying</span><span class="p">():</span>
            <span class="c1"># destroy the context and socket</span>
            <span class="n">context</span><span class="o">.</span><span class="n">release_readlock</span><span class="p">()</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">cleanup_error_context</span><span class="p">(</span><span class="n">context</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
                <span class="k">pass</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_epoll</span><span class="o">.</span><span class="n">modify</span><span class="p">(</span>
                <span class="n">context</span><span class="o">.</span><span class="n">get_sock</span><span class="p">()</span><span class="o">.</span><span class="n">fileno</span><span class="p">(),</span> <span class="n">select</span><span class="o">.</span><span class="n">EPOLLIN</span> <span class="o">|</span> <span class="n">select</span><span class="o">.</span><span class="n">EPOLLET</span>
            <span class="p">)</span>
            <span class="n">context</span><span class="o">.</span><span class="n">release_readlock</span><span class="p">()</span>

<div class="viewcode-block" id="CConnectionManager.read"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.conn.CConnectionManager.read">[docs]</a>    <span class="k">def</span> <span class="nf">read</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">context</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        read with conn context</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">context</span><span class="o">.</span><span class="n">is_detroying</span><span class="p">():</span>
            <span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;The context is being destroyed. return&#39;</span><span class="p">)</span>
            <span class="k">return</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">context</span><span class="o">.</span><span class="n">try_readlock</span><span class="p">():</span>
            <span class="k">return</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_do_read</span><span class="p">(</span><span class="n">context</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_finish_read_callback</span><span class="p">(</span><span class="kc">True</span><span class="p">,</span> <span class="n">context</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
            <span class="n">context</span><span class="o">.</span><span class="n">to_destroy</span><span class="p">()</span>
            <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;read error occur, error type:</span><span class="si">{0}</span><span class="s1">, content:</span><span class="si">{1}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="nb">type</span><span class="p">(</span><span class="n">error</span><span class="p">),</span> <span class="n">error</span><span class="p">)</span>
            <span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">cleanup_error_context</span><span class="p">(</span><span class="n">context</span><span class="p">)</span>
            <span class="n">log</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="n">traceback</span><span class="o">.</span><span class="n">format_exc</span><span class="p">())</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_finish_read_callback</span><span class="p">(</span><span class="kc">False</span><span class="p">,</span> <span class="n">context</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">_do_read</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">context</span><span class="p">):</span>
        <span class="n">sock</span> <span class="o">=</span> <span class="n">context</span><span class="o">.</span><span class="n">get_sock</span><span class="p">()</span>
        <span class="n">data</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">context</span><span class="o">.</span><span class="n">move2recving_msg</span><span class="p">()</span>
        <span class="k">while</span> <span class="bp">self</span><span class="o">.</span><span class="n">_stopsign</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">True</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">data</span> <span class="o">=</span> <span class="n">sock</span><span class="o">.</span><span class="n">recv</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">NET_RW_SIZE</span><span class="p">)</span>
            <span class="k">except</span> <span class="n">socket</span><span class="o">.</span><span class="n">error</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
                <span class="n">err</span> <span class="o">=</span> <span class="n">error</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                <span class="k">if</span> <span class="n">err</span> <span class="o">==</span> <span class="n">errno</span><span class="o">.</span><span class="n">EAGAIN</span><span class="p">:</span>
                    <span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span>
                        <span class="s1">&#39;EAGAIN happend, peer info </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span>
                        <span class="n">context</span><span class="o">.</span><span class="n">get_context_info</span><span class="p">()</span>
                    <span class="p">)</span>
                    <span class="k">return</span> <span class="n">context</span>
                <span class="k">elif</span> <span class="n">err</span> <span class="o">==</span> <span class="n">errno</span><span class="o">.</span><span class="n">EWOULDBLOCK</span><span class="p">:</span>
                    <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span>
                        <span class="s1">&#39;EWOULDBLOCK happend, context info </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span>
                        <span class="n">context</span><span class="o">.</span><span class="n">get_context_info</span><span class="p">()</span>
                    <span class="p">)</span>
                    <span class="k">return</span> <span class="n">context</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span>
                        <span class="s1">&#39;Socket error happend, error:</span><span class="si">%s</span><span class="s1">,  peer info </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span>
                        <span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">error</span><span class="p">),</span> <span class="n">context</span><span class="o">.</span><span class="n">get_context_info</span><span class="p">())</span>
                    <span class="p">)</span>
                    <span class="n">context</span><span class="o">.</span><span class="n">to_destroy</span><span class="p">()</span>
                    <span class="k">return</span> <span class="n">context</span>
            <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
                <span class="n">log</span><span class="o">.</span><span class="n">critical</span><span class="p">(</span>
                    <span class="s1">&#39;Socket error happend, error:</span><span class="si">%s</span><span class="s1">,  peer info </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span>
                    <span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">error</span><span class="p">),</span> <span class="n">context</span><span class="o">.</span><span class="n">get_context_info</span><span class="p">())</span>
                <span class="p">)</span>
                <span class="n">context</span><span class="o">.</span><span class="n">to_destroy</span><span class="p">()</span>
                <span class="k">return</span> <span class="n">context</span>
            <span class="n">data_len</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">data_len</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="c1"># socket closed by peer</span>
                <span class="n">context</span><span class="o">.</span><span class="n">to_destroy</span><span class="p">()</span>
                <span class="k">return</span> <span class="n">context</span>
            <span class="n">context</span><span class="o">.</span><span class="n">do_recv_data</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">data_len</span><span class="p">)</span>
            <span class="k">del</span> <span class="n">data</span>

    <span class="k">def</span> <span class="nf">_finish_write_callback</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">succ</span><span class="p">,</span> <span class="n">result</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;finish write callback&quot;&quot;&quot;</span>
        <span class="n">context</span> <span class="o">=</span> <span class="n">result</span>
        <span class="c1"># You cannot do things below as getpeername will block if the conn</span>
        <span class="c1"># has problem!!!!!   - Guannan</span>
        <span class="c1"># try:</span>
        <span class="c1">#     context.get_sock().getpeername()</span>
        <span class="c1"># except socket.error as error:</span>
        <span class="c1">#   log.debug(&#39;Seems socket failed to getpeername:%s&#39; % str(error))</span>
        <span class="c1">#   context.to_destroy()</span>
        <span class="k">if</span> <span class="n">context</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">context</span><span class="o">.</span><span class="n">is_detroying</span><span class="p">():</span>
            <span class="c1"># destroy the context and socket</span>
            <span class="n">context</span><span class="o">.</span><span class="n">release_writelock</span><span class="p">()</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">cleanup_error_context</span><span class="p">(</span><span class="n">context</span><span class="p">)</span>
            <span class="c1"># pylint: disable=W0703</span>
            <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
                <span class="n">log</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s1">&#39;context destroying encounters error,&#39;</span>
                    <span class="s1">&#39;skip it:</span><span class="si">{0}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">error</span><span class="p">)</span>
                <span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># log.debug(&#39;to epoll modify&#39;)</span>
            <span class="n">epoll_write_params</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_epoll_write_params</span><span class="p">()</span>
            <span class="n">context</span><span class="o">.</span><span class="n">release_writelock</span><span class="p">()</span>

    <span class="c1"># context hash locked the writing.</span>
    <span class="c1"># guarantee there&#39;s only 1 thread for context writing.</span>
    <span class="k">def</span> <span class="nf">_handle_new_send</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">context</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        handle new send message</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">context</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;conetext is none&#39;</span><span class="p">)</span>
            <span class="k">return</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_thdpool</span><span class="o">.</span><span class="n">add_1job</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">add_write_job</span><span class="p">,</span> <span class="n">context</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_do_write</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">context</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;write into interface sending buffer&quot;&quot;&quot;</span>
        <span class="n">sock</span> <span class="o">=</span> <span class="n">context</span><span class="o">.</span><span class="n">get_sock</span><span class="p">()</span>
        <span class="n">msg</span> <span class="o">=</span> <span class="n">context</span><span class="o">.</span><span class="n">try_move2next_sending_msg</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">msg</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;send queue is empty, quit the _do_write thread&#39;</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">context</span>
        <span class="c1"># log.debug(&#39;To enter write loop until eagin&#39;)</span>
        <span class="c1"># pylint:disable=w0212</span>
        <span class="k">while</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_stopsign</span><span class="p">:</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">msg</span><span class="o">.</span><span class="n">get_write_bytes</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">NET_RW_SIZE</span><span class="p">)</span>
            <span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;msg get_write_bytes_len to be sent: </span><span class="si">%d</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">))</span>
            <span class="k">try</span><span class="p">:</span>

                <span class="n">succ_len</span> <span class="o">=</span> <span class="n">sock</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
                <span class="n">msg</span><span class="o">.</span><span class="n">seek_write</span><span class="p">(</span><span class="n">succ_len</span><span class="p">)</span>
            <span class="k">except</span> <span class="n">cuperr</span><span class="o">.</span><span class="n">AsyncMsgError</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
                <span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;has seek out of msg len, continue&#39;</span><span class="p">)</span>
            <span class="k">except</span> <span class="n">socket</span><span class="o">.</span><span class="n">error</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
                <span class="n">err</span> <span class="o">=</span> <span class="n">error</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                <span class="k">if</span> <span class="n">err</span> <span class="o">==</span> <span class="n">errno</span><span class="o">.</span><span class="n">EAGAIN</span><span class="p">:</span>
                    <span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span>
                        <span class="s1">&#39;EAGAIN happend, context info </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span>
                        <span class="n">context</span><span class="o">.</span><span class="n">get_context_info</span><span class="p">()</span>
                    <span class="p">)</span>
                    <span class="k">return</span> <span class="n">context</span>
                <span class="k">elif</span> <span class="n">err</span> <span class="o">==</span> <span class="n">errno</span><span class="o">.</span><span class="n">EWOULDBLOCK</span><span class="p">:</span>
                    <span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span>
                        <span class="s1">&#39;EWOULDBLOCK happend, context info </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span>
                        <span class="n">context</span><span class="o">.</span><span class="n">get_context_info</span><span class="p">()</span>
                    <span class="p">)</span>
                    <span class="k">return</span> <span class="n">context</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">log</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span>
                        <span class="s1">&#39;Socket error happend. But its not eagin,error:</span><span class="si">%s</span><span class="s1">,</span><span class="se">\</span>
<span class="s1">                        context info </span><span class="si">%s</span><span class="s1">, errno:</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span>
                        <span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">error</span><span class="p">),</span> <span class="n">context</span><span class="o">.</span><span class="n">get_context_info</span><span class="p">(),</span> <span class="n">err</span><span class="p">)</span>
                    <span class="p">)</span>
                    <span class="n">context</span><span class="o">.</span><span class="n">to_destroy</span><span class="p">()</span>
                    <span class="k">break</span>
            <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
                <span class="n">log</span><span class="o">.</span><span class="n">error</span><span class="p">(</span>
                    <span class="s1">&#39;Socket error happend, error:</span><span class="si">%s</span><span class="s1">,  context info </span><span class="si">%s</span><span class="s1">, trace:</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span>
                    <span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">error</span><span class="p">),</span> <span class="n">context</span><span class="o">.</span><span class="n">get_context_info</span><span class="p">(),</span> <span class="n">traceback</span><span class="o">.</span><span class="n">format_exc</span><span class="p">())</span>
                <span class="p">)</span>
                <span class="n">context</span><span class="o">.</span><span class="n">to_destroy</span><span class="p">()</span>
                <span class="k">break</span>
            <span class="k">finally</span><span class="p">:</span>
                <span class="k">del</span> <span class="n">data</span>
            <span class="k">if</span> <span class="n">msg</span><span class="o">.</span><span class="n">is_msg_already_sent</span><span class="p">():</span>
                <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span>
                    <span class="s1">&#39;sent out a msg uniqid:</span><span class="si">{0}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                        <span class="n">async_msg</span><span class="o">.</span><span class="n">netmsg_tostring</span><span class="p">(</span><span class="n">msg</span><span class="p">))</span>
                <span class="p">)</span>
                <span class="c1"># if we have successfully send out a msg. Then move to next one</span>
                <span class="n">msg</span> <span class="o">=</span> <span class="n">context</span><span class="o">.</span><span class="n">try_move2next_sending_msg</span><span class="p">()</span>
                <span class="k">if</span> <span class="n">msg</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="k">break</span>
        <span class="k">return</span> <span class="n">context</span>

<div class="viewcode-block" id="CConnectionManager.add_write_job"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.conn.CConnectionManager.add_write_job">[docs]</a>    <span class="k">def</span> <span class="nf">add_write_job</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">context</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        add network write into queue</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">context</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">peerinfo</span> <span class="o">=</span> <span class="n">context</span><span class="o">.</span><span class="n">get_peerinfo</span><span class="p">()</span>
        <span class="c1"># pylint: disable=W0703</span>
        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
            <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;failed to get peerinfo, return&#39;</span><span class="p">)</span>
            <span class="k">return</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">context</span><span class="o">.</span><span class="n">try_writelock</span><span class="p">():</span>
            <span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span>
                <span class="s1">&#39;Another thread is writing the context, return. &#39;</span>
                <span class="s1">&#39;Peerinfo:</span><span class="si">%s</span><span class="s1">:</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span>
                <span class="p">(</span><span class="n">peerinfo</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">peerinfo</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
            <span class="p">)</span>
            <span class="k">return</span>
        <span class="k">if</span> <span class="n">context</span><span class="o">.</span><span class="n">is_detroying</span><span class="p">():</span>
            <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span>
                <span class="s1">&#39;The context is being destroyed, i will do nothing. &#39;</span>
                <span class="s1">&#39;Peerinfo:</span><span class="si">%s</span><span class="s1">:</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span>
                <span class="p">(</span><span class="n">peerinfo</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">peerinfo</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
            <span class="p">)</span>
            <span class="k">return</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="c1"># log.debug(&#39;write in add_write_job&#39;)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_do_write</span><span class="p">(</span><span class="n">context</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_finish_write_callback</span><span class="p">(</span><span class="kc">True</span><span class="p">,</span> <span class="n">context</span><span class="p">)</span>
        <span class="c1"># pylint: disable=W0703</span>
        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
            <span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span>
                <span class="s1">&#39;seems error happend for context:</span><span class="si">%s</span><span class="s1"> Peerinfo:</span><span class="si">%s</span><span class="s1">:</span><span class="si">%s</span><span class="se">\n</span><span class="s1">, </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span>
                <span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">error</span><span class="p">),</span> <span class="n">peerinfo</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">peerinfo</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">traceback</span><span class="o">.</span><span class="n">format_exc</span><span class="p">())</span>
            <span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_finish_write_callback</span><span class="p">(</span><span class="kc">False</span><span class="p">,</span> <span class="n">context</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">_get_resend_msg_key</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ip</span><span class="p">,</span> <span class="n">port</span><span class="p">,</span> <span class="n">uniq_id</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;generate resend msg key&quot;&quot;&quot;</span>
        <span class="n">key</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">{0}</span><span class="s1">_</span><span class="si">{1}</span><span class="s1">_</span><span class="si">{2}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">ip</span><span class="p">,</span> <span class="n">port</span><span class="p">,</span> <span class="n">uniq_id</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">key</span>

    <span class="k">def</span> <span class="nf">_check_needack_queue</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        check needack_queue</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;start check needack_queue&#39;</span><span class="p">)</span>
        <span class="n">msg_item</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">ack_flag</span> <span class="o">=</span> <span class="n">async_msg</span><span class="o">.</span><span class="n">MSG_FLAG2NUM</span><span class="p">[</span><span class="s1">&#39;FLAG_ACK&#39;</span><span class="p">]</span>
        <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
            <span class="n">msg_item</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">msg_item</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_needack_context_queue</span><span class="o">.</span><span class="n">get_nowait</span><span class="p">()</span>
            <span class="k">except</span> <span class="n">queue</span><span class="o">.</span><span class="n">Empty</span><span class="p">:</span>
                <span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;no need ack msg found yet&#39;</span><span class="p">)</span>
                <span class="k">break</span>
            <span class="n">ack_success</span> <span class="o">=</span> <span class="kc">False</span>
            <span class="n">toaddr</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="n">uniq_id</span> <span class="o">=</span> <span class="n">msg_item</span><span class="o">.</span><span class="n">get_uniq_id</span><span class="p">()</span>
            <span class="n">toaddr</span> <span class="o">=</span> <span class="n">msg_item</span><span class="o">.</span><span class="n">get_to_addr</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">msg_item</span><span class="o">.</span><span class="n">get_flag</span><span class="p">()</span> <span class="o">&amp;</span> <span class="n">ack_flag</span> <span class="o">==</span> <span class="n">ack_flag</span><span class="p">:</span>
                <span class="c1"># if msg_item is a ack msg</span>
                <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span>
                    <span class="s1">&#39;msgack received, stop resending &#39;</span>
                    <span class="s1">&#39;msguniq_id:</span><span class="si">{0}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">uniq_id</span><span class="p">)</span>
                <span class="p">)</span>
                <span class="n">msg_item</span><span class="o">.</span><span class="n">set_resend_flag</span><span class="p">(</span><span class="n">async_msg</span><span class="o">.</span><span class="n">MSG_RESEND_SUCCESS</span><span class="p">)</span>
                <span class="n">toaddr</span> <span class="o">=</span> <span class="n">msg_item</span><span class="o">.</span><span class="n">get_from_addr</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>
                <span class="n">ack_success</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="n">to_ip</span> <span class="o">=</span> <span class="n">toaddr</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
            <span class="n">to_port</span> <span class="o">=</span> <span class="n">toaddr</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
            <span class="n">msg_key</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_resend_msg_key</span><span class="p">(</span><span class="n">to_ip</span><span class="p">,</span> <span class="n">to_port</span><span class="p">,</span> <span class="n">uniq_id</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">ack_success</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">msg_key</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_needack_context_dict</span><span class="p">:</span>
                    <span class="n">last_msg</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_needack_context_dict</span><span class="p">[</span><span class="n">msg_key</span><span class="p">]</span>
                    <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">_needack_context_dict</span><span class="p">[</span><span class="n">msg_key</span><span class="p">]</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_executor</span><span class="o">.</span><span class="n">queue_exec</span><span class="p">(</span>
                        <span class="n">last_msg</span><span class="o">.</span><span class="n">get_callback_function</span><span class="p">(),</span>
                        <span class="n">executor</span><span class="o">.</span><span class="n">URGENCY_NORMAL</span><span class="p">,</span>
                        <span class="n">last_msg</span><span class="p">,</span> <span class="kc">True</span>
                    <span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">log</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span>
                        <span class="s1">&#39;got duplicate ack-msg, msg_id:</span><span class="si">{0}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">uniq_id</span><span class="p">)</span>
                    <span class="p">)</span>
                <span class="k">continue</span>
            <span class="c1"># not ack_success + not in  context_dict</span>
            <span class="k">if</span> <span class="n">msg_key</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_needack_context_dict</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_needack_context_dict</span><span class="p">[</span><span class="n">msg_key</span><span class="p">]</span> <span class="o">=</span> <span class="n">msg_item</span>
        <span class="n">time_out_list</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_needack_context_dict</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
            <span class="n">msg_item</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_needack_context_dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
            <span class="n">msg_flag</span> <span class="o">=</span> <span class="n">msg_item</span><span class="o">.</span><span class="n">get_resend_flag</span><span class="p">()</span>
            <span class="n">msg_info</span> <span class="o">=</span> <span class="s1">&#39;msg_type:</span><span class="si">%d</span><span class="s1">, msg_flag:</span><span class="si">%d</span><span class="s1">, msg_dest:</span><span class="si">%s</span><span class="s1">,uniqid:</span><span class="si">%d</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span>
                <span class="n">msg_item</span><span class="o">.</span><span class="n">get_msg_type</span><span class="p">(),</span>
                <span class="n">msg_item</span><span class="o">.</span><span class="n">get_flag</span><span class="p">(),</span>
                <span class="nb">str</span><span class="p">(</span><span class="n">msg_item</span><span class="o">.</span><span class="n">get_to_addr</span><span class="p">()),</span>
                <span class="n">msg_item</span><span class="o">.</span><span class="n">get_uniq_id</span><span class="p">()</span>
            <span class="p">)</span>
            <span class="k">if</span> <span class="n">msg_flag</span> <span class="o">==</span> <span class="n">async_msg</span><span class="o">.</span><span class="n">MSG_RESEND_SUCCESS</span><span class="p">:</span>
                <span class="n">time_out_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
                <span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span>
                    <span class="s1">&#39;del succ-msg from resending queue: </span><span class="si">{0}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">msg_info</span><span class="p">)</span>
                <span class="p">)</span>
            <span class="k">elif</span> <span class="n">msg_flag</span> <span class="o">==</span> <span class="n">async_msg</span><span class="o">.</span><span class="n">MSG_RESENDING_FLAG</span><span class="p">:</span>
                <span class="n">msg_total_timeout</span> <span class="o">=</span> <span class="n">msg_item</span><span class="o">.</span><span class="n">get_total_timeout</span><span class="p">()</span>
                <span class="c1"># if msg resending timeouts</span>
                <span class="k">if</span> <span class="n">msg_total_timeout</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="n">msg_item</span><span class="o">.</span><span class="n">set_resend_flag</span><span class="p">(</span><span class="n">async_msg</span><span class="o">.</span><span class="n">MSG_TIMEOUT_TO_DELETE</span><span class="p">)</span>
                    <span class="n">log</span><span class="o">.</span><span class="n">error</span><span class="p">(</span>
                        <span class="s1">&#39;timeout, failed to get ack for netmsg:</span><span class="si">{0}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                            <span class="n">msg_info</span><span class="p">)</span>
                    <span class="p">)</span>
                    <span class="n">time_out_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">msg_last_retry_time</span> <span class="o">=</span> <span class="n">msg_item</span><span class="o">.</span><span class="n">get_last_retry_time</span><span class="p">()</span>
                    <span class="n">msg_retry_interval</span> <span class="o">=</span> <span class="n">msg_item</span><span class="o">.</span><span class="n">get_retry_interval</span><span class="p">()</span>
                    <span class="n">now</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
                    <span class="n">elapse_time</span> <span class="o">=</span> <span class="n">now</span> <span class="o">-</span> <span class="n">msg_last_retry_time</span>
                    <span class="k">if</span> <span class="n">elapse_time</span> <span class="o">&gt;=</span> <span class="n">msg_retry_interval</span><span class="p">:</span>
                        <span class="c1"># update total_timeout</span>
                        <span class="n">msg_item</span><span class="o">.</span><span class="n">set_total_timeout</span><span class="p">(</span>
                            <span class="n">msg_total_timeout</span> <span class="o">-</span> <span class="n">elapse_time</span>
                        <span class="p">)</span>
                        <span class="n">msg_item</span><span class="o">.</span><span class="n">set_last_retry_time</span><span class="p">(</span><span class="n">now</span><span class="p">)</span>
                        <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;to resend CNeedAckMsg: </span><span class="si">{0}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">msg_info</span><span class="p">))</span>
                        <span class="n">msg_item</span><span class="o">.</span><span class="n">pre_resend</span><span class="p">()</span>
                        <span class="n">msg_item</span><span class="o">.</span><span class="n">add_retry_times</span><span class="p">()</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">push_msg2sendqueue</span><span class="p">(</span><span class="n">msg_item</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">time_out_list</span><span class="p">:</span>
            <span class="n">msg_item</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_needack_context_dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
            <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">_needack_context_dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_executor</span><span class="o">.</span><span class="n">queue_exec</span><span class="p">(</span>
                <span class="n">msg_item</span><span class="o">.</span><span class="n">get_callback_function</span><span class="p">(),</span>
                <span class="n">executor</span><span class="o">.</span><span class="n">URGENCY_NORMAL</span><span class="p">,</span>
                <span class="n">msg_item</span><span class="p">,</span> <span class="kc">False</span>
            <span class="p">)</span>

<div class="viewcode-block" id="CConnectionManager.do_check_msg_ack_loop"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.conn.CConnectionManager.do_check_msg_ack_loop">[docs]</a>    <span class="k">def</span> <span class="nf">do_check_msg_ack_loop</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        check msg ack loop</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;start check msg ack info.&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_check_needack_queue</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_executor</span><span class="o">.</span><span class="n">delay_exec</span><span class="p">(</span>
            <span class="mi">3</span><span class="p">,</span>   <span class="c1"># todo set the check_time to ?</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">do_check_msg_ack_loop</span><span class="p">,</span>
            <span class="n">urgency</span><span class="o">=</span><span class="n">executor</span><span class="o">.</span><span class="n">URGENCY_HIGH</span>
        <span class="p">)</span></div></div>

<span class="c1"># vi:set tw=0 ts=4 sw=4 nowrap fdm=indent</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, CUP-DEV.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../../../../',
            VERSION:'1.7.0',
            LANGUAGE:'en',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../../../../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../../../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../../../../_static/doctools.js"></script>
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

  

  <script type="text/javascript" src="../../../../../_static/js/theme.js"></script>

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>